package controller.expertInfo.handleLeaderCheck;

import base.response.OffsetLimitPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.LeaderCheckServices;
import servicesImp.expertInfo.LeaderCheckUtil;
import wt.entity.expertInfo.LeaderCheck;
import wt.entity.expertInfo.LeaderCheckExample;
import wt.staticData.ShortData;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/2.
 */
@Controller

@RequestMapping("expertInfo/handleLeaderCheck")
public class HandleLeaderCheckController {
    @Autowired
    LeaderCheckServices leaderCheckServices;


    @RequiresPermissions("expertInfo:handleLeaderCheck:default")
    @RequestMapping("index")
    public String index() {
        return "expertInfo/handleLeaderCheck/index";
    }

    @RequiresPermissions("expertInfo:handleLeaderCheck:default")
    @RequestMapping("select")
    @ResponseBody
    public OffsetLimitPage select(String expertName, String entityName, Integer offset, Integer limit) throws Exception {

        LeaderCheckExample leaderCheckExample = new LeaderCheckExample();
        leaderCheckExample.setOrderByClause("id desc");
        LeaderCheckExample.Criteria criteria = leaderCheckExample.createCriteria();
        criteria.andWorkFlowStepEqualTo(ShortData.s4);
        if (!StringUtils.isBlank(expertName)) {
            criteria.andExpertNameLike("%" + expertName.trim() + "%");
        }

        if (!StringUtils.isBlank(entityName)) {
            criteria.andTableNameEqualTo(entityName);
        }

        OffsetLimitPage offsetLimitPage = leaderCheckServices.selectByExample(leaderCheckExample, offset, limit);
        return offsetLimitPage;
    }

    @RequiresPermissions("expertInfo:handleLeaderCheck:default")
    @RequestMapping("setResult")
    @ResponseBody
    public CommonResult setResult(String id) throws Exception {
        CommonResult commonResult = new CommonResult();
        LeaderCheck leaderCheck = leaderCheckServices.selectByPrimaryKey(id);
        if (leaderCheck.getWorkFlowStep() != ShortData.s4) {
            commonResult.setMessage("数据已被处理");
            return commonResult;
        }
        LeaderCheckUtil.handleCheckResult(leaderCheck);
        commonResult.setStatus(leaderCheckServices.updateByPrimaryKey(leaderCheck));
        return commonResult;
    }


    @RequiresPermissions("expertInfo:handleLeaderCheck:default")
    @ResponseBody
    @RequestMapping("welcomeTips")
    public CommonResult welcomeTips() throws Exception {
        CommonResult commonResult = new CommonResult();
        LeaderCheckExample leaderCheckExample = new LeaderCheckExample();
        LeaderCheckExample.Criteria criteria = leaderCheckExample.createCriteria();
        criteria.andWorkFlowStepEqualTo(ShortData.s4);
        int count = leaderCheckServices.countByExample(leaderCheckExample);
        if (count == 0) {
            return commonResult.setError();
        } else {
            Map map = new HashMap();
            map.put("info", count + "条领导复查意见待处理");
            map.put("url", "expertInfo/handleLeaderCheck/index");
            commonResult.setData(map);
            return commonResult.setSuccess();
        }
    }


}
